<template>
  <div class="index">
    <van-tabs v-model="active">
      <van-tab v-for="item in channelList" :key="item.id" :title="item.name">
        <ArticleList :channel="item" />
      </van-tab>
    </van-tabs>

    <!-- 按钮 -->
    <div class="bar-btn" @click="isShowChannelEdit = true">
      <van-icon name="wap-nav" />
    </div>

    <!-- 弹层 -->
    <van-action-sheet v-model="isShowChannelEdit" title="频道管理">
      <ChannelEdit
        :channelList="channelList"
        @update-active="hUpdateActive"
        @change-channel="hChangeChannel"
        :curIndex="active"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import ArticleList from './articleList/articleList.vue'
import { getChannels } from '@/api/channel.js'
import ChannelEdit from './channelEdit.vue'
export default {
  name: 'home',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channelList: [],
      isShowChannelEdit: false

    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    hUpdateActive () {
      this.active--
    },
    async loadChannels () {
      try {
        const res = await getChannels()
        // console.log(res)
        this.channelList = res.data.data.channels
        console.log(this.channelList)
      } catch (err) {
        console.log(err)
      }
    },
    hChangeChannel (curIdx) {
      console.log('收到点击事件', curIdx)
      this.active = curIdx
      this.isShowChannelEdit = false
    }
  }

}
</script>

<style lang='less' scoped>
.bar-btn {
  position: fixed;
  right: 5px;
  top: 57px;
  display: flex;
  align-items: center;
  background-color: #fff;
  opacity: 0.8;
  z-index: 1;
  .van-icon-wap-nav {
    font-size: 20px;
  }
}
</style>
